<?xml version="1.0" encoding="UTF-8"?>
<!--
	Application context definition for PetClinic on JPA.
-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- ========================= RESOURCE DEFINITIONS ========================= -->

	<!--
		Activates a load-time weaver for the context. Any bean within the context that
		implements LoadTimeWeaverAware (such as LocalContainerEntityManagerFactoryBean)
		will receive a reference to the autodetected load-time weaver.
	-->
	<context:load-time-weaver/>

	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!--
		Uses Apache Commons DBCP for connection pooling. See Commons DBCP documentation
		for the required JAR files. Alternatively you can use another connection pool
		such as C3P0, similarly configured using Spring.
	-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
			p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}"
			p:password="${jdbc.password}"/>

	<!-- JNDI DataSource for JEE environments -->
	<!--
	<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/petclinic"/>
	-->

	<!-- JPA EntityManagerFactory -->
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
			p:dataSource-ref="dataSource">
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter"
					p:databasePlatform="${jpa.databasePlatform}" p:showSql="${jpa.showSql}"/>
			<!--
			<bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"
					p:database="${jpa.database}" p:showSql="${jpa.showSql}"/>
			-->
			<!--
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
					p:database="${jpa.database}" p:showSql="${jpa.showSql}"/>
			-->
		</property>
	</bean>

	<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
			p:entityManagerFactory-ref="entityManagerFactory"/>


	<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->

	<!--
		Activates various annotations to be detected in bean classes: Spring's
		@Required and @Autowired, as well as JSR 250's @PostConstruct,
		@PreDestroy and @Resource (if available) and JPA's @PersistenceContext
		and @PersistenceUnit (if available).
	-->
	<context:annotation-config/>

	<!--
		Instruct Spring to perform declarative transaction management
		automatically on annotated classes.
	-->
	<tx:annotation-driven mode="aspectj"/>

	<!--
		Simply defining this bean will cause requests to owner names to be saved.
		This aspect is defined in petclinic.jar's META-INF/aop.xml file.
		Note that we can dependency inject this bean like any other bean.
	-->
	<bean class="org.springframework.samples.petclinic.aspects.UsageLogAspect" p:historySize="300"/>

	<!--
		Post-processor to perform exception translation on @Repository classes (from native
		exceptions such as JPA PersistenceExceptions to Spring's DataAccessException hierarchy).
	-->
	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

	<!--
		Will automatically be transactional due to @Transactional.
		EntityManager will be auto-injected due to @PersistenceContext.
		PersistenceExceptions will be auto-translated due to @Repository.
	-->
	<bean id="clinic" class="org.springframework.samples.petclinic.jpa.EntityManagerClinic"/>

</beans>
